﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Security;
using System.Security.Cryptography;

namespace Crypt_Tranceiver
{
    public class file_cryptor
    {
        SymmetricAlgorithm salg = null;
        byte[] rgbKey = null;
        byte[] rgbIV = null;


        public file_cryptor(String key, String iv, bool encryt)
        {
            rgbKey = sha256_encoding(key);//生成加密密钥
            rgbIV = md5_encoding(iv);//生成IV

            salg = SymmetricAlgorithm.Create();//创建加密器
            salg.Mode = CipherMode.CBC;//指定加密模式
            salg.Padding = PaddingMode.ISO10126;
        }


        /// <summary>
        /// SHA256 加密字符串
        /// </summary>
        /// <param name="rawPass">源字符串</param>
        /// <returns>加密后字符串</returns>
        public byte[] sha256_encoding(string rawPass)
        {
            // 创建SHA256类的默认实例：MD5CryptoServiceProvider
            SHA256 hash = SHA256.Create();
            byte[] bs = Encoding.UTF8.GetBytes(rawPass);
            byte[] hs = hash.ComputeHash(bs);
            hash.Clear();
            return hs;
        }

        /// <summary>
        /// MD5 加密字符串
        /// </summary>
        /// <param name="rawPass">源字符串</param>
        /// <returns>加密后字符串</returns>
        public byte[] md5_encoding(string rawPass)
        {
            // 创建MD5类的默认实例：MD5CryptoServiceProvider
            MD5 hash = MD5.Create();
            byte[] bs = Encoding.UTF8.GetBytes(rawPass);
            byte[] hs = hash.ComputeHash(bs);
            hash.Clear();
            return hs;
        }


        /// <summary>
        /// 对文件内容进行AES加密
        /// </summary>
        /// <param name="sourceFile">待加密的文件绝对路径</param>
        /// <param name="destFile">加密后的文件保存的绝对路径</param>
        public void EncryptFile(string sourceFile, string destFile)
        {
            if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在！", sourceFile);

            byte[] btFile = File.ReadAllBytes(sourceFile);//读取文件内容

            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    using (CryptoStream cs = new CryptoStream(fs, salg.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                    {
                        cs.Write(btFile, 0, btFile.Length);//写入原始数据
                        cs.FlushFinalBlock();//更新流
                    }
                }
                catch
                {
                    throw;//上报异常
                }
                finally
                {
                    fs.Close();//关闭文件流
                }
            }
        }

        /// <summary>
        /// 对文件内容进行AES解密
        /// </summary>
        /// <param name="sourceFile">待解密的文件绝对路径</param>
        /// <param name="destFile">解密后的文件保存的绝对路径</param>
        public void DecryptFile(string sourceFile, string destFile)
        {
            if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在！", sourceFile);

            byte[] btFile = File.ReadAllBytes(sourceFile);//读取文件内容

            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    using (CryptoStream cs = new CryptoStream(fs, salg.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))//创建解密流
                    {
                        cs.Write(btFile, 0, btFile.Length);//写入待解密数据
                        cs.FlushFinalBlock();//更新流
                    }
                }
                catch
                {
                    throw;//上报异常
                }
                finally
                {
                    fs.Close();//关闭文件流
                }
            }
        }

    }
}